/*
 * 同城信息发布
 * author: huangbin
 * 技术支持微信号：wxid_i8yk4fp0uep322
 * 开源协议：MIT
 *  *Copyright (c) 2018 https:/www.tczhb.club All rights reserved.
 */

var Api = require('../../utils/api.js');
var util = require('../../utils/util.js');
var auth = require('../../utils/auth.js');
var WxParse = require('../../wxParse/wxParse.js');
var wxApi = require('../../utils/wxApi.js')
var wxRequest = require('../../utils/wxRequest.js')
var app = getApp();
var pageCount = app.globalData.page_size ? app.globalData.page_size : 5;

Page({
  data: {
    isLastPage: false,
    commentList: [],
    dataType: 0,//(0获取我的评论1回复我的)
    page: 1,
  },
  onLoad: function (options) {
    var self = this;
    if (!app.globalData.isGetOpenid) {
      auth.getUsreInfo();
    }else{
      self.fetchMyCommentData();
    }
  },
  //获取我的评论数据
  fetchMyCommentData: function(){
    var self = this;
    var data = {};
    data.page = self.data.page;
    data.tb = 'portal_post';
    data.limit = pageCount;
    wx.showLoading({
      title: '正在加载',
      mask: true
    });
    var dataUrl;
    if (self.data.dataType == 0){
      dataUrl = Api.getWeixinComment();
    } else if (self.data.dataType == 1){
      dataUrl = Api.getChildrenComments();
    }
    var getCommentsRequest = wxRequest.getRequest(dataUrl,data);
    getCommentsRequest
      .then(response => {
        if (response.statusCode === 200) {
          if (response.data.code === 200) {
            if (response.data.data.length < pageCount) {
              self.setData({
                isLastPage: true
              });
            }
            self.setData({
              commentList: self.data.commentList.concat(response.data.data.map(function (item) {
                var strdate = item.create_time;
                item.dateStr = util.getDateDiff(strdate);
                var strSummary = util.removeHTML(item.content);
                item.summary = strSummary;
                item.post.post_content = util.removeHTML(item.post.post_content);
                return item;
              })),
            });
            wx.hideLoading();
          }
        }
      })
  },
  //切换数据列表
  swichCommentData: function(e){
    var that = this;
    var current_id = e.currentTarget.dataset.current;
    if (that.data.dataType === current_id) {
      return false;
    } else {
      that.setData({
        dataType: current_id,
        commentList: [],
        isLastPage: false,
        page: 1
      });
      that.fetchMyCommentData();
    }
  },
  // 跳转至查看文章详情
  redictDetail: function (e) {
    // console.log('查看文章');
    var id = e.currentTarget.id,
      url = '../detail/detail?id=' + id;
    wx.navigateTo({
      url: url
    })
  },
  //加载分页
  loadMore: function (e) {
    var self = this;
    if (!self.data.isLastPage) {
      self.setData({
        page: self.data.page + 1
      });
      this.fetchMyCommentData();
    }
  },
  onReachBottom: function () {
    var self = this;
    this.loadMore();
  }
})



